﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;
using AGuang.Script.TypeConvert;
using AGuang.WebTool;

[assembly: WebResource("AGuang.Script.AGuang_Client.Controls.Tab.js", "text/javascript")]
namespace AGuang.Script {
    public class Tab:BaseScript {
        private String[] buttons;
        private String[] contents;
        private String buttonCss;
        private String activeCss;
        private String onTabChange;
        private bool autoScript;
        private String changeMode;
        private int defaultIndex = -1;

        protected override void OnLoad(EventArgs e) {
            ClientScriptManager csm = Page.ClientScript;
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.Controls.Tab.js"));
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            if (!AutoScript) return;

            List<String> realButtons = FindControls(Buttons);
            List<String> realContents = FindControls(Contents);
            if (realButtons.Count != realContents.Count) throw new Exception("按钮与内容的个数不一致");
            List<String> buttonCss=new List<string>();
            buttonCss.Add(ButtonCssClass);
            buttonCss.Add(ActiveButtonCssClass);

            StringBuilder sb = new StringBuilder();
            String jsVar="s" + this.ClientID;
            sb.AppendFormat("var {0}=new Page.Tab({1},{2},{3});",jsVar , JSON.Object2Json(realButtons), JSON.Object2Json(realContents), JSON.Object2Json(buttonCss));
            if (!String.IsNullOrEmpty(ChangeMethod)) {
                sb.AppendFormat("{0}.SetChangeMethod('{1}');", jsVar, ChangeMethod);
            }
            if (!String.IsNullOrEmpty(OnTabChange)) {
                sb.AppendFormat("{0}.Add_TabChange({1});", jsVar, OnTabChange);
            }
            if (defaultIndex != -1) {
                sb.AppendFormat("{0}.ChangeIndex({1});", jsVar, defaultIndex);
            }
            AddEffect(sb.ToString());
        }

        private List<String> FindControls(String[] ids) {
            List<String> list = new List<string>();
            foreach (String id in ids) {
                if (String.IsNullOrEmpty(id)) continue;
                String b = id.Trim();
                Control c = this.Parent.FindControl(b);
                if (c != null) list.Add(c.ClientID);
                else list.Add(b);
            }
            return list;
        }

        /// <summary>
        /// 按钮列表
        /// </summary>
        [TypeConverter(typeof(TypeConvert.ArrayConverter))]
        public String[] Buttons {
            get { return buttons; }
            set { buttons = value; }
        }

        /// <summary>
        /// 内容区列表
        /// </summary>
        [TypeConverter(typeof(TypeConvert.ArrayConverter))]
        public String[] Contents {
            get { return contents; }
            set { contents = value; }
        }

        /// <summary>
        /// 按钮的css
        /// </summary>
        public String ButtonCssClass {
            get { return buttonCss; }
            set { buttonCss = value; }
        }

        /// <summary>
        /// 被激活的按钮的css
        /// </summary>
        public String ActiveButtonCssClass {
            get { return activeCss; }
            set { activeCss = value; }
        }

        /// <summary>
        /// 切换选项卡时产生的js事件
        /// </summary>
        public String OnTabChange {
            get { return onTabChange; }
            set { onTabChange = value; }
        }

        /// <summary>
        /// 按何种方式切换选项卡
        /// </summary>
        [TypeConverter(typeof(TabChangeMethodConverter))]
        public String ChangeMethod {
            get { return changeMode; }
            set { changeMode = value; }
        }

        /// <summary>
        /// 启用自动生成脚本
        /// </summary>
        public bool AutoScript {
            get { return autoScript; }
            set { autoScript = value; }
        }

        /// <summary>
        /// 默认选择项，从0开始
        /// </summary>
        public int DefaultIndex {
            get { return defaultIndex; }
            set { defaultIndex = value; }
        }

        public String JsVar {
            get { return "s" + this.ClientID; }
        }
    }
}
